package cn.snmp.mutual.uk.event;

import java.util.*;

import lombok.extern.slf4j.Slf4j;
import cn.snmp.mutual.uk.stack.SnmpContextv3;


@Slf4j
public class RawPduReceivedSupport {
    static int j;
    public static final String version_id = "@(#)$Id: RawPduReceivedSupport.java,v 1.5 2006/02/09 14:30:18 birgit Exp $ Copyright Westhawk Ltd";
    private Object source;
    private transient Vector pduListeners;

    public RawPduReceivedSupport(final Object src) {
        this.source = src;
    }

    public synchronized void empty() {
        if (this.pduListeners != null) {
            this.pduListeners.removeAllElements();
        }
    }

    public synchronized int getListenerCount() {
        int c = 0;
        if (this.pduListeners != null) {
            c = this.pduListeners.size();
        }
        return c;
    }

    public synchronized void addRawPduListener(final RawPduListener listener) {
        if (this.pduListeners == null) {
            this.pduListeners = new Vector(5);
        }
        if (!this.pduListeners.contains(listener)) {
            this.pduListeners.addElement(listener);
        }
    }

    public synchronized void removeRawPduListener(final RawPduListener listener) {
        if (this.pduListeners != null) {
            this.pduListeners.removeElement(listener);
        }
    }

    public boolean fireRawPduReceived(final int version, final String hostAddress, final int hostPort, final byte[] message) {
        boolean isConsumed = false;
        Vector copyOfListeners = null;
        if (this.pduListeners != null) {
            synchronized (this.pduListeners) {
                copyOfListeners = (Vector) this.pduListeners.clone();
            }
        }
        if (copyOfListeners != null) {
            final int l = message.length;
            final int sz = copyOfListeners.size();
            RawPduEvent evt;
            for (int i = sz - 1; i >= 0 && !isConsumed; isConsumed = evt.isConsumed(), --i) {
                final RawPduListener listener = (RawPduListener) copyOfListeners.elementAt(i);
                final byte[] copyOfMessage = new byte[l];
                System.arraycopy(message, 0, copyOfMessage, 0, l);
                evt = new RawPduEvent(this.source, version, hostAddress, copyOfMessage, hostPort);
                final Hashtable table = (Hashtable) this.getSnmpConfig(evt.getHostAddress());
                System.out.println(table);
                try {
                    final String username = (String) table.get((Object) "UN");
                    final String AP = (String) table.get((Object) "AP");
                    final String AW = (String) table.get((Object) "AW");
                    final String PP = (String) table.get((Object) "PP");
                    final String PW = (String) table.get((Object) "PW");
                    final String SL = (String) table.get((Object) "SL");
                    final String[] SLarr = SL.split(",");
                    final SnmpContextv3 context = (SnmpContextv3) listener;
                    if ("Auth".equals(SLarr[0])) {
                        context.setUseAuthentication(true);
                    } else {
                        context.setUseAuthentication(false);
                    }
                    if ("Priv".equals(SLarr[1])) {
                        context.setUsePrivacy(true);
                    } else {
                        context.setUsePrivacy(false);
                    }
                    if ("SHA".equals(AP)) {
                        context.setAuthenticationProtocol(1);
                        context.setUserAuthenticationPassword(AW);
                    } else if ("MD5".equals(AP)) {
                        context.setAuthenticationProtocol(0);
                        context.setUserAuthenticationPassword(AW);
                    }
                    if ("DES".equals(PP)) {
                        context.setPrivacyProtocol(2);
                        context.setUserPrivacyPassword(PW);
                    } else if ("AES".equals(PP)) {
                        context.setPrivacyProtocol(3);
                        context.setUserPrivacyPassword(PW);
                    }
                    context.setUserName(username);
                    context.setContextName("");
                    context.setContextEngineId(new byte[0]);
                } catch (Exception e) {
                    log.error(" query  V3 info is error");
                    log.error("" + e);
                }
                listener.rawPduReceived(evt);
            }
        }
        return isConsumed;
    }

    public Hashtable getSnmpConfig(final String hostAddress) {
        if (hostAddress == null || hostAddress.equalsIgnoreCase("")) {
            return null;
        }
        final Hashtable table = new Hashtable();
      /*  final Connection conn = DBConnectionManager.getConnection();
        Statement st = null;
        ResultSet rs = null;
        try {
            final String sql = "SELECT  ircnetnodeid , vers , v3  FROM RCNETNODE where MANAGED_MODE = 1 and IPADDRESS = '" + hostAddress + "'";
            st = conn.createStatement();
            rs = st.executeQuery(sql);
            while (rs.next()) {
                final String ircnetnodeid = rs.getString("ircnetnodeid");
                final String vers = rs.getString("vers");
                final String v3 = rs.getString("v3");
                final String[] strS = v3.split(";");
                for (int i = 0; i < strS.length; ++i) {
                    final int index = strS[i].indexOf("=");
                    final String key = strS[i].substring(0, index);
                    final String value = strS[i].substring(index + 1, strS[i].length());
                    table.put((Object)key, (Object)value);
                }
                rs.close();
                st.close();
            }
        }
        catch (Exception e) {
            log.error(" query  V3 info is error");
        }
        finally {
            try {
                conn.close();
            }
            catch (Exception ex) {}
        }*/
        return table;
    }

    static {
        RawPduReceivedSupport.j = 0;
    }
}
